package com.rem.elasticsearch.apidemo.doc;

import org.apache.http.HttpHost;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;

import java.io.IOException;

import static org.apache.http.HttpHost.DEFAULT_SCHEME_NAME;

/**
 * 查询单个文档
 *
 * @author Rem
 * @date 2021-05-30
 */

public class DocGet {

    public static void main(String[] args) throws IOException {
        // 创建ES客户端
        RestHighLevelClient esClient = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost", 9200, DEFAULT_SCHEME_NAME)));
        // 查询文档 - 请求对象
        GetRequest request = new GetRequest();
        // 设置索引及唯一性标识
        request.index("user").id("2006");

        GetResponse response = esClient.get(request, RequestOptions.DEFAULT);

        //3.打印结果信息
        System.out.println("_result:" + response.toString());

        // 关闭ES客户端
        esClient.close();
    }
}
